package sysConfig

import (
	"io/ioutil"
	"log"

	"gopkg.in/yaml.v2"
)

var AllConfig *YAMLConfig

type AppYAMLConfig struct {
	AppPort string `yaml:"port"`
}
type KafkaYAMLConfig struct {
	Enabled bool     `yaml:"enabled"`
	Servers []string `yaml:"servers"`
	Topic   string   `yaml:"topic"`
	GroupId string   `yaml:"groupId"`
}

type SmsYAMLConfig struct {
	Url      string `yaml:"url"`
	Account  string `yaml:"account"`
	Extno    string `yaml:"extno"`
	Password string `yaml:"password"`
}

type YAMLConfig struct {
	App       AppYAMLConfig       `yaml:"app"`
	Kafka     KafkaYAMLConfig     `yaml:"kafka"`
	Sms       SmsYAMLConfig       `yaml:"sms"`
	Websocket WebsocketYAMLConfig `yaml:"websocket"`
	Platform  PlatformYAMLConfig  `yaml:"platform"`
}
type WebsocketYAMLConfig struct {
	ServerUrl string `yaml:"serverUrl"`
	HassUrl   string `yaml:"hassUrl"`
	HassToken string `yaml:"hassToken"`
}

type PlatformYAMLConfig struct {
	Url      string `yaml:"url"`
	ClientId string `yaml:"clientId"`
	Secret   string `yaml:"secret"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Key      string `yaml:"key"`
}

func LoadYAMLConfig(path string) *YAMLConfig {
	// 定义局部变量
	config := &YAMLConfig{}

	// 打开并读取yaml配置文件
	yamlFile, err := ioutil.ReadFile(path)
	if err != nil {
		log.Printf("Open config.yaml failed!")
		panic(err)
	}

	// 使用yaml中Unmarshal方法，解析yaml配置文件并绑定定义的结构体
	err = yaml.Unmarshal(yamlFile, config)
	if err != nil {
		log.Printf("yaml config file Unmarsha failed!")
		panic(err)
	}

	return config
}
